﻿<%@ Page Title="" Language="C#" MasterPageFile="~/NestedMasterPage.master" %>

<%@ Register TagPrefix="Page" TagName="Nav" Src="~/UserControls/PageControl.ascx" %>
<%@ Register TagPrefix="Page" TagName="SiteMap" Src="~/UserControls/SiteMapControl.ascx" %>

<script runat="server">


  protected void Page_Load(object sender, EventArgs e)
  {
    if (Page.IsPostBack == false)
    {
      //初始化檢視筆數的顏色為第一筆
      this.LinkButton1.CssClass = "current";

      //初始化查詢數量與排序
      Session["ListCount"] = null;
      Session["IsOrderAesc"] = null;

      //產生清單
      GenProductList();
    }

  }


  private void GenProductList(string listCount = "15", string isOrderAesc = "0")
  {
    //一般館別
    string Prod_Cat_ID = CommonUtil.GetRequestValue("category");
    //string Prod_Cat_ID = "3_1";
    //設定一般館別Banner
    if (!string.IsNullOrEmpty(Prod_Cat_ID))
    {
      //呈現SiteMap
      SiteMapControl.Prod_Cat_ID = Prod_Cat_ID;

      switch (Prod_Cat_ID.Substring(0, 1))
      {
        case "1":
          imgBanner.Src = "~/Images/banners/SimpleBanner/EVA_AIR_ONLY.jpg";
          break;
        case "2":
          imgBanner.Src = "~/Images/banners/SimpleBanner/BOUTIQUE.jpg";
          break;
        case "3":
          imgBanner.Src = "~/Images/banners/SimpleBanner/ELECTRONIC.jpg";
          break;
        case "4":
          imgBanner.Src = "~/Images/banners/SimpleBanner/LIFE_STYLE.jpg";
          break;
        case "5":
          imgBanner.Src = "~/Images/banners/SimpleBanner/BEAUTY.jpg";
          break;
        case "6":
          imgBanner.Src = "~/Images/banners/SimpleBanner/GOURMET_FEAST.jpg";
          break;
        case "8":
          imgBanner.Src = "~/Images/banners/SimpleBanner/DISCOUNT.jpg";
          break;
        default:
          imgBanner.Src = "~/Images/banners/SimpleBanner/EVA_AIR_ONLY.jpg";
          break;
      }
    }

    //館別新品
    string NewCatID = CommonUtil.GetRequestValue("NewCatID");
    //設定館別新品Banner
    if (!string.IsNullOrEmpty(NewCatID))
    {
      //館別新品-隱藏排序和查詢
      FilterArea.Visible = false;
      Nav1.Visible = false;
      Nav2.Visible = false;

      //呈現SiteMap
      SiteMapControl.NewCatID = NewCatID;

      switch (NewCatID)
      {
        case "6":
          imgBanner.Src = "~/Images/banners/SimpleBanner/EVA_AIR_ONLY.jpg";
          break;
        case "7":
          imgBanner.Src = "~/Images/banners/SimpleBanner/BOUTIQUE.jpg";
          break;
        case "8":
          imgBanner.Src = "~/Images/banners/SimpleBanner/ELECTRONIC.jpg";
          break;
        case "9":
          imgBanner.Src = "~/Images/banners/SimpleBanner/LIFE_STYLE.jpg";
          break;
        case "10":
          imgBanner.Src = "~/Images/banners/SimpleBanner/BEAUTY.jpg";
          break;
        case "11":
          imgBanner.Src = "~/Images/banners/SimpleBanner/GOURMET_FEAST.jpg";
          break;
        case "13":
          imgBanner.Src = "~/Images/banners/SimpleBanner/DISCOUNT.jpg";
          break;
        default:
          imgBanner.Src = "~/Images/banners/SimpleBanner/EVA_AIR_ONLY.jpg";
          break;
      }
    }

    string ListCount = Session["ListCount"] == null ? listCount : Session["ListCount"].ToString();
    string IsOrderAesc = Session["IsOrderAesc"] == null ? isOrderAesc : Session["IsOrderAesc"].ToString();
    int PageNumber = this.Nav1.currentPageNumber;
    int StartNumber = 1 + Convert.ToInt32(ListCount) * (PageNumber - 1);
    int EndNumber = StartNumber + Convert.ToInt32(ListCount) - 1;

    string jsonString;
    if (!string.IsNullOrEmpty(NewCatID))
    {
      B2C_MALL_HOME ws = new B2C_MALL_HOME();
      ws.Url = ConfigurationManager.AppSettings["B2C_MALL_HOME"].ToString();
      jsonString =
        ws.B2C_MALL_getBoards(System.Configuration.ConfigurationManager.AppSettings["WebDeviceid"].ToString()
        , HttpContext.Current.User.Identity.Name, NewCatID);
    }
    else
    {
      B2C_MALL_GetProductList_0 ws = new B2C_MALL_GetProductList_0();
      ws.Url = ConfigurationManager.AppSettings["B2C_MALL_GetProductList_0"].ToString();
      jsonString =
        ws.B2C_MALL_GetProductList(System.Configuration.ConfigurationManager.AppSettings["WebDeviceid"].ToString()
        , HttpContext.Current.User.Identity.Name, Prod_Cat_ID
        , StartNumber.ToString(), EndNumber.ToString(), IsOrderAesc
        , MinAmount.Value, MaxAmount.Value);
    }

    JObject restoredObject = JsonConvert.DeserializeObject<JObject>(jsonString);

    if (restoredObject != null)
    {
      string Is_Valid = restoredObject["Is_Valid"].ToString();
      string msg = restoredObject["Msg"].ToString();

      if (Is_Valid == "Y")
      {
        if (!string.IsNullOrEmpty(NewCatID))
        {
          dynamic dynObj = restoredObject["Prd_Ary"];
          this.ListViewProductList.DataSource = dynObj;
          this.ListViewProductList.DataBind();
          this.Nav1.TotalCount = this.Nav2.TotalCount = restoredObject["Prd_Ary"].ToList().Count.ToString();
          this.Nav1.PagesCount = this.Nav2.PagesCount =
              Math.Ceiling((Convert.ToDecimal(restoredObject["Prd_Ary"].ToList().Count.ToString()) / Convert.ToDecimal(ListCount))).ToString();          
        }
        else
        {
          dynamic dynObj = restoredObject["ProductList_Ary"];
          this.ListViewProductList.DataSource = dynObj;
          this.ListViewProductList.DataBind();
          this.Nav1.TotalCount = this.Nav2.TotalCount = restoredObject["Total_Count"].ToString();
          this.Nav1.PagesCount = this.Nav2.PagesCount =
              Math.Ceiling((Convert.ToDecimal(restoredObject["Total_Count"]) / Convert.ToDecimal(ListCount))).ToString();
        }
      }
      else
      {
        this.ListViewProductList.DataSource = null;
        this.ListViewProductList.DataBind();
        this.Nav1.TotalCount = this.Nav2.TotalCount = "0";
        this.Nav1.PagesCount = this.Nav2.PagesCount = "0";
      }
      //不滿5頁的去除其頁數
      this.Nav1.DisplayPageNumber();
      this.Nav2.DisplayPageNumber();
    }
  }

  protected void LinkButton2_Click(object sender, EventArgs e)
  {
    //將檢視筆數的顏色先清除
    foreach (Control item in this.numsPlaceHolder.Controls)
    {
      if (item is LinkButton)
      {
        ((LinkButton)item).CssClass = "";
      }
    }

    //從第一頁開始
    this.Nav1.currentPageNumber = 1;
    this.Nav2.currentPageNumber = 1;

    //將postback的檢視筆數設定顏色        
    LinkButton selectButton = ((LinkButton)sender);
    selectButton.CssClass = "current";

    //存Session
    Session["ListCount"] = selectButton.Text;

    //產生產品清單
    GenProductList(selectButton.Text);
  }

  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
    //存Session
    Session["IsOrderAesc"] = DropDownList1.SelectedValue;

    //取得排序條件並產生產品清單 (0:預設1:正向2:反向)
    GenProductList(isOrderAesc: DropDownList1.SelectedValue);
  }

  protected void Nav1_OnPageSelected(object sender)
  {
    if (sender == this.Nav1)
    {
      this.Nav2.currentPageNumber = this.Nav1.currentPageNumber;
    }
    else
    {
      this.Nav1.currentPageNumber = this.Nav2.currentPageNumber;
    }
    //取得欲檢視頁面號碼並產生產品清單 
    GenProductList();

  }

  protected void lb_search_Click(object sender, EventArgs e)
  {
    GenProductList();
  }

  protected void ListViewProductList_ItemDataBound(object sender, ListViewItemEventArgs e)
  {
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
      if (CommonUtil.GetRequestValue("NewCatID") == "13")
      {
        e.Item.FindControl("divMember").Visible  = false ;
        
        dynamic Prd_Ary = e.Item.DataItem;
        string Output_Prod_Cat_ID = Prd_Ary.Output_Prod_Cat_ID.ToString();
        switch (Output_Prod_Cat_ID)
        {
          case "1"://1:哩上加禮
            e.Item.FindControl("divMileage").Visible = true;
            (e.Item.FindControl("Lit_MileagePrice") as Literal).Text = Prd_Ary.ACT_PRICE.ToString("n0");
            string CASH = Prd_Ary.CASH != null ? Prd_Ary.CASH.ToString("n0") : Prd_Ary.ACT_PRICE.ToString("n0");
            string MILEAGE = Prd_Ary.MILEAGE != null ? Prd_Ary.MILEAGE.ToString() : "0";
            (e.Item.FindControl("Lit_MILEAGE") as Literal).Text = CASH + " + " + MILEAGE;
            break;
          case "3"://3:限時折扣(整點特賣)
            e.Item.FindControl("divClock").Visible = true;
            string Time_Dsc_Price = Prd_Ary.Time_Dsc_Price != null ? Prd_Ary.Time_Dsc_Price.ToString("n0") : Prd_Ary.ACT_PRICE.ToString("n0");
            (e.Item.FindControl("Lit_TimeDscPrice") as Literal).Text = Time_Dsc_Price;
            string START_range = Prd_Ary.START_DATE_range != null ? Prd_Ary.START_DATE_range.ToString("yyyy/MM/dd") : "2014/2/1";
            string START_DATE_range = Prd_Ary.START_DATE_range != null ? Prd_Ary.START_DATE_range.ToString("t") : "00:00:00";
            string END_DATE_range = Prd_Ary.END_DATE_range != null ? Prd_Ary.END_DATE_range.ToString("t") : "12:00:00";
            (e.Item.FindControl("Lit_DATE_range") as Literal).Text = START_range + " " + START_DATE_range + "~" + END_DATE_range;
            break;
          case "4"://4:期間限定
            e.Item.FindControl("divPeriod").Visible = true;
            string RangeDscPrice = Prd_Ary.RangeDscPrice != null ? Prd_Ary.RangeDscPrice.ToString("n0") : Prd_Ary.ACT_PRICE.ToString("n0");
            (e.Item.FindControl("Lit_RangeDscPrice") as Literal).Text = RangeDscPrice;
            string START_DATE = Prd_Ary.START_DATE != null ? Prd_Ary.START_DATE.ToString("yyyy/MM/dd") : "2014/2/1";
            string END_DATE = Prd_Ary.END_DATE != null ? Prd_Ary.END_DATE.ToString("yyyy/MM/dd") : "9999/12/31";
            (e.Item.FindControl("Lit_DATE") as Literal).Text = START_DATE + " ~ " + END_DATE;
            break;
          default: //0:一般
            e.Item.FindControl("divMember").Visible = true;
            break;
        }
      }      
    }
  }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="NestedContent" runat="server">
  <div class="simple-banner">
    <img id="imgBanner" src="~/Images/banners/SimpleBanner/Simple1.jpg" runat="server" />
  </div>
  <div class="breadcrumbs">
    <Page:SiteMap runat="server" ID="SiteMapControl" />
    <%--<a href="#">首頁</a> / <a href="#" class="current">寰宇精品</a>--%>
  </div>
  <div class="filter-area" id="FilterArea" runat="server">
    <div class="filter-left">
      <div class="slide-filter">
        <p>
          價位區間 : 
          <span id="amount">$0 - $20000 以上</span>
          <asp:LinkButton ID="lb_search" runat="server" CssClass="btn fr" Text="確 定" OnClick="lb_search_Click" />
          <asp:HiddenField ID="MinAmount" runat="server" ClientIDMode="Static" />
          <asp:HiddenField ID="MaxAmount" runat="server" ClientIDMode="Static" />
        </p>
        <div class="slider-range"></div>
        <script src="<%= Page.ResolveUrl("~/Scripts/jquery-ui-1.10.3.custom.min.js")%>"></script>
        <script>
          $(".slider-range").slider({
            range: true,
            min: 0,
            max: 20000,
            step: 100,
            values: [0, 20000],
            slide: function (event, ui) {
              if (ui.values[1] == 20000) {
                $("#amount").text("$" + ui.values[0] + " - $" + ui.values[1] + " 以上");
                $("#MaxAmount").val("999999");
              }
              else {
                $("#amount").text("$" + ui.values[0] + " - $" + ui.values[1]);
                $("#MaxAmount").val(ui.values[1]);
              }
              $("#MinAmount").val(ui.values[0]);
            }
          });

          //C# Page_Load
          if ($("#MinAmount").val() != "" && $("#MaxAmount").val() != "") {
            $(".slider-range").slider("values", 0, $("#MinAmount").val());
            $(".slider-range").slider("values", 1, $("#MaxAmount").val());
            if ($("#MaxAmount").val() >= 20000) {
              $("#amount").text("$" + $("#MinAmount").val() + " - $20000 以上");
            }
            else {
              $("#amount").text("$" + $("#MinAmount").val() + " - $" + $("#MaxAmount").val());
            }
          }
        </script>
      </div>
    </div>
    <div class="filter-mid">
      <div class="drop-filter">
        排序方式                
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
          <asp:ListItem Value="0">請選擇</asp:ListItem>
          <asp:ListItem Value="1">價格由低到高</asp:ListItem>
          <asp:ListItem Value="2">價格由高到低</asp:ListItem>
        </asp:DropDownList>
      </div>
    </div>
    <div class="filter-right">
      <div class="num-filter">
        每頁顯示商品數  
        <div class="nums">
          <asp:PlaceHolder ID="numsPlaceHolder" runat="server">
            <%--<a href="#" class="current">15</a> / <a href="#">30</a> / <a href="#">60</a>--%>
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton2_Click">15</asp:LinkButton>
            / 
                <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">30</asp:LinkButton>
            / 
                <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton2_Click">60</asp:LinkButton>
          </asp:PlaceHolder>
        </div>
      </div>
    </div>
  </div>

  <!-- Page Nav User Control -->
  <Page:Nav ID="Nav1" runat="server" OnOnPageSelected="Nav1_OnPageSelected" />

  <%--<div class="product-list">
        <div class="product-item">
            <div class="item-image">
                <a href="#">
                    <img src="~/Images/Products/product1.jpg" runat="server" />
                </a>
            </div>
            <div class="release">
                <span class="new">NEW</span>
                <span class="hot">HOT</span>
            </div>
            <div class="description">
                <div class="desc-top">
                    <p>特價 $ <strong>21,760</strong></p>
                </div>
                <div class="desc-bottom">
                    <h6>RIMOWA--CLASSIC FLIGHT</h6>
                    <p>
                        復古系列小型二輪旅行箱63
                    </p>
                    <p>
                        復古系列小型二輪旅行箱63
                    </p>
                </div>
            </div>
        </div>
        <div class="product-item">
            <div class="item-image">
                <a href="#">
                    <img src="~/Images/Products/product2.jpg" runat="server" />
                </a>
            </div>
            <div class="release">
                <span class="new">NEW</span>
                <span class="hot">HOT</span>
            </div>
            <div class="description">
                <div class="desc-top">
                    <p>特價 $ <strong>21,760</strong></p>
                </div>
                <div class="desc-bottom">
                    <p>
                        復古系列小型二輪旅行箱63
                        復古系列小型二輪旅行箱63
                        復古系列小型二輪旅行箱63
                    </p>

                </div>
            </div>
        </div>

    </div>--%>

  <asp:ListView ID="ListViewProductList" runat="server" OnItemDataBound="ListViewProductList_ItemDataBound">
    <LayoutTemplate>
      <div id="itemPlaceholderContainer" runat="server" class="product-list">
        <li runat="server" id="itemPlaceholder" />
      </div>
    </LayoutTemplate>
    <ItemTemplate>
      <div class="product-item">
        <div class="item-image">
          <a href='<%# Server.HtmlEncode(Eval("B2C_CODE","~/Products/ProductDetail.aspx?B2C_CODE={0}").ToString()) %>' runat="server">
            <img id="Img1" src='<%# Server.HtmlEncode(Eval("IMAGE_PATH_M").ToString()) %>' runat="server" />
          </a>
        </div>
        <div class="release">
          <%# CommonUtil.GetNEWAndHOT(Server.HtmlEncode(Eval("light_status").ToString())) %>
        </div>
        <div class="description">
          <div class="desc-top" id="divMember" runat="server">
            <p>特價 $ <strong><%# Server.HtmlEncode(Eval("ACT_PRICE","{0:N0}").ToString()) %></strong></p>
            <%# CommonUtil.GetMemberPrice(Server.HtmlEncode(Eval("MbrPrice").ToString())) %>
          </div>
          <div class="desc-top" id="divPeriod" runat="server" Visible="false" >
            期間限定價 $ <strong>
              <asp:Literal ID="Lit_RangeDscPrice" runat="server" /></strong>
            <b>
              <p class="orange-text">
                <asp:Literal ID="Lit_DATE" runat="server" />
              </p>
            </b>
          </div>
          <div class="desc-top" id="divClock" runat="server" Visible="false" >
            限時優惠價 $ <strong>
              <asp:Literal ID="Lit_TimeDscPrice" runat="server"/></strong>
            <b>
              <p class="orange-text">
                <asp:Literal ID="Lit_DATE_range" runat="server" />
              </p>
            </b>
          </div>
          <div class="desc-top" id="divMileage" runat="server" Visible="false" >
            特價 $ <strong>
              <asp:Literal ID="Lit_MileagePrice" runat="server" /></strong>
            <b>
              <p class="orange-text">
                或 $
          <asp:Literal ID="Lit_MILEAGE" runat="server" />
                哩
              </p>
            </b>
          </div>
          <div class="desc-bottom">
            <p><%# Server.HtmlEncode(Eval("B2C_CODE_NAME").ToString()) %></p>
          </div>
        </div>
      </div>
    </ItemTemplate>
  </asp:ListView>
  <!-- #Product List -->

  <!-- Page Nav User Control -->
  <Page:Nav ID="Nav2" runat="server" OnOnPageSelected="Nav1_OnPageSelected" />


  <div class="go-to-top">
    <a href="#top">回到頂端</a>
  </div>
</asp:Content>
